package com.zp.qualitypractice.Adapter;

import android.content.Context;
import android.content.pm.ProviderInfo;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.sax.RootElement;
import android.support.annotation.IdRes;
import android.text.util.Linkify;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

/**
 * Created by izp on 2017/4/10.
 */

public class SwipeViewBaseViewHolder {

    private SparseArray<View> mViews;
    private Context mContext;
    private int mPosition;
    private View mItemView;
    private int mItemLayoutId;

    public SwipeViewBaseViewHolder(Context mContext, int mPosition, ViewGroup parent, int mItemLayoutId) {
        this.mContext = mContext;
        this.mPosition = mPosition;
        this.mItemLayoutId = mItemLayoutId;
        mViews = new SparseArray<>();
        mItemView = LayoutInflater.from(mContext).inflate(mItemLayoutId,parent,false);
        mItemView.setTag(this);//在构造方法中直接创建rootView然后设置tag的还是第一次见

    }
    public static SwipeViewBaseViewHolder get(Context context,int mPosition,ViewGroup viewGroup,
                                              View itemView,int mItemLayoutId){
        if (null == itemView){
            return new SwipeViewBaseViewHolder(context,mPosition,viewGroup,mItemLayoutId);
        }else {
            SwipeViewBaseViewHolder viewHolder = (SwipeViewBaseViewHolder) itemView.getTag();
            viewHolder.mPosition = mPosition;//取出缓存的itemView并更新当前的position
            return  viewHolder;
        }
    }

    public int getmPosition(){
        return mPosition;
    }


    public int getmItemLayoutId(){
        return mItemLayoutId;
    }

    public View getmItemView(){
        return mItemView;
    }
    /**
     * 根据id 找view，但是这个泛型方法是不安全的
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getViewByID(@IdRes int viewId){
        View view = mViews.get(viewId);
        if (null==view){
            view = mItemView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T)view;
    }

    /**
     * @param viewId
     * @return
     * 避免泛型强转
     */
    public View getView(@IdRes int viewId){
        View view = mViews.get(viewId);
        if (null==view){
            view = mItemView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return view;
    }

    public SwipeViewBaseViewHolder setText(int viewId, String text) {
        TextView tv = getViewByID(viewId);
        tv.setText(text);
        return this;
    }

    public SwipeViewBaseViewHolder setTextView(int viewId, String text) {
        if (getView(viewId) instanceof TextView){
            TextView tv = (TextView) getView(viewId);
            tv.setText(text);
        }
        return this;
    }

    public SwipeViewBaseViewHolder setImageResource(int viewId, int resId) {
        ImageView view = getViewByID(viewId);
        view.setImageResource(resId);
        return this;
    }

    public SwipeViewBaseViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = getViewByID(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }

    public SwipeViewBaseViewHolder setImageDrawable(int viewId, Drawable drawable) {
        ImageView view = getViewByID(viewId);
        view.setImageDrawable(drawable);
        return this;
    }

    public SwipeViewBaseViewHolder setBackgroundColor(int viewId, int color) {
        View view = getView(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    public SwipeViewBaseViewHolder setBackgroundRes(int viewId, int backgroundRes) {
        View view = getView(viewId);
        view.setBackgroundResource(backgroundRes);
        return this;
    }

    public SwipeViewBaseViewHolder setTextColor(int viewId, int textColor) {
        TextView view = getViewByID(viewId);
        view.setTextColor(textColor);
        return this;
    }

    public SwipeViewBaseViewHolder setTextColorRes(int viewId, int textColorRes) {
        TextView view = getViewByID(viewId);
        view.setTextColor(mContext.getResources().getColor(textColorRes));
        return this;
    }

//    @SuppressLint("NewApi")
    public SwipeViewBaseViewHolder setAlpha(int viewId, float value) {
        getView(viewId).setAlpha(value);
        return this;
    }


    public SwipeViewBaseViewHolder setVisible(int viewId, boolean visible) {
        View view = getViewByID(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public SwipeViewBaseViewHolder linkify(int viewId) {
        TextView view = getViewByID(viewId);
        Linkify.addLinks(view, Linkify.ALL);
        return this;
    }

    public SwipeViewBaseViewHolder setTypeface(Typeface typeface, int... viewIds) {
        for (int viewId : viewIds) {
            TextView view = getViewByID(viewId);
            view.setTypeface(typeface);
            view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        }
        return this;
    }

    public SwipeViewBaseViewHolder setProgress(int viewId, int progress) {
        ProgressBar view = getViewByID(viewId);
        view.setProgress(progress);
        return this;
    }

    public SwipeViewBaseViewHolder setProgress(int viewId, int progress, int max) {
        ProgressBar view = getViewByID(viewId);
        view.setMax(max);
        view.setProgress(progress);
        return this;
    }

    public SwipeViewBaseViewHolder setMax(int viewId, int max) {
        ProgressBar view = getViewByID(viewId);
        view.setMax(max);
        return this;
    }

    public SwipeViewBaseViewHolder setRating(int viewId, float rating) {
        RatingBar view = getViewByID(viewId);
        view.setRating(rating);
        return this;
    }

    public SwipeViewBaseViewHolder setRating(int viewId, float rating, int max) {
        RatingBar view = getViewByID(viewId);
        view.setMax(max);
        view.setRating(rating);
        return this;
    }

    public SwipeViewBaseViewHolder setTag(int viewId, Object tag) {
        View view = getViewByID(viewId);
        view.setTag(tag);
        return this;
    }

    public SwipeViewBaseViewHolder setTag(int viewId, int key, Object tag) {
        View view = getViewByID(viewId);
        view.setTag(key, tag);
        return this;
    }

    public SwipeViewBaseViewHolder setChecked(int viewId, boolean checked) {
        Checkable view = (Checkable) getViewByID(viewId);
        view.setChecked(checked);
        return this;
    }


    public SwipeViewBaseViewHolder setOnClickListener(int viewId,
                                         View.OnClickListener listener) {
        View view = getView(viewId);
        view.setOnClickListener(listener);
        return this;
    }

    public SwipeViewBaseViewHolder setOnTouchListener(int viewId,
                                         View.OnTouchListener listener) {
        View view = getView(viewId);
        view.setOnTouchListener(listener);
        return this;
    }

    public SwipeViewBaseViewHolder setOnLongClickListener(int viewId,
                                             View.OnLongClickListener listener) {
        View view = getView(viewId);
        view.setOnLongClickListener(listener);
        return this;
    }

}
